import { setting } from '@/api/app'
import { to } from 'await-to-js'
import { ProfitVo, SystemVo, TenantVo } from '@/api/app/types'
import defaultSettings from '@/settings'
import { Locale } from '@/types/store'
import sysI18n from '@/lang'
import constants from '@/constants'

export const useAppStore = defineStore('app', () => {
  const sidebarStatus = useStorage('sidebarStatus', '1')
  const sidebar = reactive({
    opened: sidebarStatus.value ? !!+sidebarStatus.value : true,
    withoutAnimation: false,
    hide: false,
  })
  const device = ref<string>('desktop')
  const size = useStorage('size', 'default')

  const pickLang = useStorage('pickLang', false)

  //后端语言
  const language = useStorage('language', defaultSettings.language)

  //后端可选择语言
  const messages = ref<Locale[]>([])

  //后端是否开启多语言
  const multipleLanguage = ref<boolean>(false)

  //app 语言
  const appLanguage = useStorage('appLanguage', defaultSettings.appLanguage)

  const tenant = ref<TenantVo>({ enable: false, defaultTenantId: constants.DEFAULT_TENANT_ID })
  const profit = ref<ProfitVo>({ enabled: false })
  const system = ref<SystemVo>({ i18n: { language: defaultSettings.language, multiple: false, locales: [] } })

  // 获取用户信息
  const getApps = async (): Promise<void> => {
    const [err, res] = await to(setting())
    if (res) {
      const app = res.data
      const charer = app.charer
      tenant.value = charer.tenant
      profit.value = charer.profit
      system.value = charer.system
      multipleLanguage.value = charer.system?.i18n?.multiple
      if (!pickLang.value) {
        language.value = charer.system?.i18n?.language
      }

      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      messages.value = charer.system?.i18n.locales.map(function (lang) {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        if (sysI18n.global.availableLocales.includes(lang.locale)) {
          const item: Locale = {
            lang: lang.locale,
            name: sysI18n.global.getLocaleMessage(lang.locale).name,
          }
          return item
        }
      })
      return Promise.resolve()
    }
    return Promise.reject(err)
  }

  const toggleSideBar = (withoutAnimation: boolean) => {
    if (sidebar.hide) {
      return false
    }

    sidebar.opened = !sidebar.opened
    sidebar.withoutAnimation = withoutAnimation
    if (sidebar.opened) {
      sidebarStatus.value = '1'
    } else {
      sidebarStatus.value = '0'
    }
  }

  const closeSideBar = ({ withoutAnimation }: any): void => {
    sidebarStatus.value = '0'
    sidebar.opened = false
    sidebar.withoutAnimation = withoutAnimation
  }
  const toggleDevice = (d: string): void => {
    device.value = d
  }
  const setSize = (s: string): void => {
    size.value = s
  }
  const toggleSideBarHide = (status: boolean): void => {
    sidebar.hide = status
  }

  const changeLanguage = (val: string): void => {
    pickLang.value = true
    language.value = val
  }

  return {
    device,
    sidebar,
    language,
    appLanguage,
    profit,
    multipleLanguage,
    messages,
    size,
    changeLanguage,
    toggleSideBar,
    closeSideBar,
    toggleDevice,
    setSize,
    toggleSideBarHide,
    getApps,
    tenant,
  }
})

export default useAppStore
